<template>
	<!-- Swiper Mode Example:
  <main-wrapper :bg-list="[recommendBanner, recommendBanner]" bg-standout>
  -->
	<view style="width: 100%;overflow: hidden;height: 100%;">
		<image :style="{marginTop:safetop+'px'}" style="position: relative;z-index:444;width: 288rpx;height: 68rpx;margin-left: 12rpx;" mode="widthFix"
			src="/static/owo/homelogo.png"></image>
		<swiper v-if="banners && banners.length" class="swiper1" circular autoplay="true">
			<swiper-item v-for="(img, index) in banners" :key="index">
				<image class="swiper-bg" :src="img" mode="widthFix" />
			</swiper-item>
		</swiper>
		<!-- <view>
			<image src="/static/owo/bannermask.png" class="bannermask"></image>
		</view> -->
		<view class="main-wrapper">
			<view class="fahuobg">
				⚠️温馨提示：商品抽奖存在概率性，付款需谨慎！
			</view>
			<view class="product">
				<view class="productitem" v-for="(e,i) in infiniteList" :key="i" @click="godetail(e)">
					<view class="productimg">
						<image class="photo" mode="widthFix" :src="e.main_photo"></image>
						<view class="huishou">
							支持80%回收
						</view>
					</view>
					<view class="product_content">
						<text class="name">{{e.name}}</text>
						<image src="/static/owo/more.png" class="more"></image>
					</view>
					<text class="desc">{{e.name}}</text>
				</view>
			</view>
			<!-- <products-swiper :data="infiniteList" /> -->
		</view>
		<tarbar :active="1" style="z-index: 333;position: relative;"></tarbar>
	</view>
</template>

<script>
	// import { ping } from '@/api/ping'
	import {
		listBanner
	} from '@/api/banner';
	import {
		listYiFanShang
	} from '@/api/yifanshang';
	import {
		listInfinite
	} from '@/api/infinite';
	import {
		listcouple
	} from '@/api/touch';
	import {
		getNewCoupons,
		takeCoupons
	} from '@/api/coupons';
	import MainWrapper from '@/wxcomponents/main-wrapper'
	import AnnouncementBoard from '@/wxcomponents/announcement-board'
	import SectionTitle from '@/wxcomponents/section-title'
	import productItem from '@/wxcomponents/product-item'
	import CouponDialogContent from '@/wxcomponents/coupon-dialog-content'
	import ProductsSwiper from '@/wxcomponents/products-swiper'
	import tarbar from '@/wxcomponents/tarbar'
	import {
		protocolone
	} from "@/api/z_meta"
	export default {
		data() {
			return {
				title: 'Hello',
				newCouponGifts: [],
				isCouponModalVisible: false,

				yiFansShangList: [],
				ouHuangShangList: [],
				infiniteList: [],
				banners: [],
				couplelist: [],
				protocol: {
					name: "",
					content: ""
				},
				safetop: 20
			}
		},

		components: {
			AnnouncementBoard,
			SectionTitle,
			productItem,
			CouponDialogContent,
			ProductsSwiper,
			MainWrapper,
			tarbar
		},
		onLoad() {
			uni.getSystemInfo({
				success: res => {
					this.safetop = res.safeAreaInsets.top;
				}
			});
		},
		onShow: async function(options) {
			const token = uni.getStorageSync("token")
			this.getprotocolone()
			await this.loadRecommendBanner()
			await this.listYiFanShang()
			await this.listOuHuangShang()
			await this.listInfinite()
			await this.listcouple()
			if (token) {
				await this.getCouponList()
			}
		},
		onPullDownRefresh: async function(options) {
			this.getprotocolone()
			await this.loadRecommendBanner()
			await this.listYiFanShang()
			await this.listOuHuangShang()
			await this.listInfinite()
			await this.listcouple()
			await this.getCouponList()
			uni.stopPullDownRefresh()
		},

		methods: {
			godetail(item) {
				uni.navigateTo({
					url: '/pages/index/donglishang?id=' + item.id
				})
			},
			getprotocolone() {
				protocolone({
					type: '首页顶部通知'
				}).then(res => {
					this.protocol = res.data.protocol
				})
			},
			closeCouponModal() {
				this.isCouponModalVisible = false;
			},
			async listcouple() {
				const params = {
					'type': '对对碰',
					'index': 1
				}

				const res = await listcouple(params)
				const data = res.data || {}
				const items = data.items || []

				for (let i = 0; i < items.length; i++) {
					items[i]['price'] = (Number(items[i]['price']) / 100).toFixed(2)
				}

				this.couplelist = items
			},
			async listYiFanShang() {
				const params = {
					'type': '一番赏',
					'index': 1
				}

				const res = await listYiFanShang(params)
				const data = res.data || {}
				const items = data.items || []

				for (let i = 0; i < items.length; i++) {
					items[i]['price'] = (Number(items[i]['price']) / 100).toFixed(2)
				}

				this.yiFansShangList = items
			},
			async listOuHuangShang() {
				const params = {
					'type': '欧皇赏'
				}

				const res = await listYiFanShang(params)
				const data = res.data || {}
				const items = data.items || []

				for (let i = 0; i < items.length; i++) {
					items[i]['price'] = (Number(items[i]['price']) / 100).toFixed(2)
				}

				this.ouHuangShangList = items
			},
			async listInfinite() {
				const res = await listInfinite()
				const data = res.data || {}
				const items = data.items || []

				for (let i = 0; i < items.length; i++) {
					items[i]['price'] = '¥' + (Number(items[i]['price']) / 100).toFixed(2)
					items[i]['unit'] = items[i]['price'] + '/抽'
				}

				this.infiniteList = items
			},

			async getCouponList() {
				const res = await getNewCoupons()
				const data = res.data || {}
				const items = data.items || []

				for (let i = 0; i < items.length; i++) {
					let showPrice = (items[i]['price'] / 100).toFixed(2)
					if (items[i]['type'] === '折扣') {
						showPrice = (items[i]['rate'] / 10).toFixed(1) + '折'
					}
					items[i]['showPrice'] = showPrice
				}

				if (items.length) {
					this.newCouponGifts = items
					this.isCouponModalVisible = true
				}
			},

			// 领取全部优惠券
			async takeAllCoupons() {
				const coupon_ids = this.newCouponGifts.map((i) => i.coupon_id)
				await takeCoupons(coupon_ids)
				this.isCouponModalVisible = false
			},

			gotoOuHuangDetail(item) {
				uni.navigateTo({
					url: '/pages/index/ouhuangshang?id=' + item.id
				})
			},
			gotoYiFanShangDetail(item) {
				uni.navigateTo({
					url: '/pages/index/yifanshang?id=' + item.id
				})
			},
			gotocoupleDetail(item) {
				uni.navigateTo({
					url: '/pages/index/touch?id=' + item.id
				})
			},
			async loadRecommendBanner() {
				const params = {
					type: '首页顶部'
				}
				const res = await listBanner(params)
				const data = res.data || {}
				const items = data.items || []
				const banners = []
				for (let i = 0; i < items.length; i++) {
					banners.push(items[i]['img'])
				}
				this.banners = banners
			}
		},
	}
</script>


<style lang="scss">
	@import 'src/static/scss/maincommon';
	.product {
		display: flex;
		flex-direction: column;
		margin-top: 28rpx;
		.productitem {
			box-sizing: border-box;
			width: 100%;
			min-height: 516rpx;
			background-image: url("/static/owo/wuxianshangbg.png");
			background-position: 0 0;
			background-repeat: no-repeat;
			background-size: cover;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			padding: 24rpx;
			margin-bottom: 24rpx;
			.productimg {
				margin-top: 20rpx;
				width: 645rpx;
				max-height: 342rpx;
				position: relative;
				.photo {
					width: 100%;
					height: 100%;
				}
				.huishou {
					position: absolute;
					right: 0;
					top: 0;
					font-size: 22rpx;
					border-radius: 0 0 0 14rpx;
					color: rgb(228 146 8);
					padding:5rpx 10rpx;
					background:linear-gradient(to bottom,rgba(255, 247, 194, 1),rgba(255, 209, 99, 1));
					
				}
			}
			.product_content {
				width: 100%;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				margin: 10rpx 0;
				.name {
					color: rgba(255, 238, 197, 1);
					font-size: 32rpx;
					font-weight: bold;
				}
				.more {
					width: 41rpx;
					height: 16rpx;
				}
			}
			.desc {
				color: rgba(255, 255, 255, 0.85);
				font-size: 28rpx;
			}
		}
	}
	.bannermask {
		width: 100%;
		position: absolute;
		left: 0;
		top: 0;
		height: 577rpx;
		z-index: 2;
	}

	.fahuobg {
		width: 702rpx;
		height: 40rpx;
		line-height: 40rpx;
		margin: 20rpx auto;
		background-image: url("/static/owo/tipbg.png");
		background-position: 0 0;
		background-repeat: no-repeat;
		background-size: 100%;
		color: rgba(254, 243, 187, 0.65);
		font-size: 20rpx;
		text-align: center;
	}

	.main-wrapper {
		/* width: 100%; */
		width: 750rpx;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		padding-left: 12px;
		padding-right: 12px;
		padding-bottom: 20px;
		box-sizing: border-box;
		padding-top: 10px;
		position: relative;
	}

	.gap-top {
		display: block;
		margin: 10rpx 0 10rpx 0;
	}

	.products-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		flex-direction: row;

		.products-list-item {
			width: 49%;
			margin-right: 2%;
		}

		.products-list-item:nth-child(2n+1) {
			margin-right: 0%;
		}
	}

	.products-list::-webkit-scrollbar {
		display: none;
		height: 0;
		width: 0;
		color: transparent;
	}

	.swiper1 {
		width: 100vw;
		height: 440rpx;
		// height: 100vh;
	}

	.swiper-bg {
		width: 100%;
	}
</style>